// Package endpoint
// Title  			user.endpoint.go
// Description  	MakeHarborProjectEndpoint端点
// Author  alittlexincan@163.com  2024/07/22 11:36
// Update  alittlexincan@163.com  2024/07/22 11:36
package endpoint

import (
	"context"
	_ "io/ioutil"
	_ "net/http"

	"github.com/go-kit/kit/endpoint"
	"xincan.com.cn/tarot-go-framework/business/user/dto"
	"xincan.com.cn/tarot-go-framework/business/user/service"
	"xincan.com.cn/tarot-go-framework/utils/middleware"
	rep "xincan.com.cn/tarot-go-framework/utils/response"
)

// MakeCreateUserEndpoint
// @Title    				MakeCreateUserEndpoint
// @Tags 					用户管理
// @Summary   		 		创建用户
// @Description   		 	创建用户，并返回用户详细信息
// @Auth      				alittlexincan@163.com         	时间（2024/07/22 11:36）
// @Update  				alittlexincan@163.com  			时间（2024/07/22 11:36）
// @Router 					/tarot/user [post]
// @Accept 					application/x-www-form-urlencoded
// @Produce 				application/json
// @Param 		name		query 		string 		true 	"用户名称" 	minlength(2) maxlength(20) 		required		default(张安)
// @Param 		password	query 		string 		true 	"用户密码" 	minlength(6) maxlength(20) 		required		default(123456)
// @Param 		role		query 		string 		true 	"用户角色" 	minlength(2) maxlength(20) 		required		default(admin)
// @Param 		type		query 		string 		true 	"用户类型" 	Enums(UNKNOWN,STUDENT,TEACHER) 	required		default(STUDENT)
// @Param 		status		query 		string 		true 	"用户状态" 	Enums(UNKNOWN,READY,EXCEPTION) 	required		default(READY)
// @Success 	200 		{object} 	response.SuccessResult{data=bool}												"正常结果信息"
// @Failure 	422 		{object} 	response.ErrorResult															"参数格式错误"
// @Failure 	429 		{object} 	response.ErrorResult															"请求过多错误"
// @Failure 	500 		{object} 	response.ErrorResult															"服务内部错误"
func MakeCreateUserEndpoint(service service.UserInterface) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		model := request.(*dto.CreateUserDto)
		ep := createUserEndpoint(model, service)
		// 此处可以继续 endPoint 增加链式调用
		ep = middleware.GlobalValidationMiddleware(model)(ep)
		return ep(ctx, request)
	}
}

func createUserEndpoint(model *dto.CreateUserDto, svc service.UserInterface) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		bus, busErr := svc.CreateUser(model)
		if busErr != nil {
			return rep.Errors(busErr, nil), nil
		}
		return rep.Success(bus), nil
	}
}

// MakeSearchUserListEndpoint
// @Title    				MakeSearchUserListEndpoint
// @Tags 					用户管理
// @Summary   		 		查询用户
// @Description   		 	根据用户ID查询用户信息，并返回用户详细信息
// @Auth      				alittlexincan@163.com         	时间（2024/07/22 11:36）
// @Update  				alittlexincan@163.com  			时间（2024/07/22 11:36）
// @Router 					/tarot/user [get]
// @Accept 					application/x-www-form-urlencoded
// @Produce 				application/json
// @Param 		id		 	query 		string 		false 	"用户ID" 	minlength(19) maxlength(19) 		required		default(1142681887794270208)
// @Success 	200 		{object} 	response.SuccessResult{data=[]vo.SearchUserListVo}									"正常结果信息"
// @Failure 	422 		{object} 	response.ErrorResult																"参数格式错误"
// @Failure 	429 		{object} 	response.ErrorResult																"请求过多错误"
// @Failure 	500 		{object} 	response.ErrorResult																"服务内部错误"
func MakeSearchUserListEndpoint(service service.UserInterface) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		model := request.(*dto.SearchUserListDto)
		ep := searchUserListEndpoint(model, service)
		// 此处可以继续 endPoint 增加链式调用
		ep = middleware.GlobalValidationMiddleware(model)(ep)
		return ep(ctx, request)
	}
}

// searchUserListEndpoint
// Title    				searchUserListEndpoint
// Description   			查询用户列表
// Param    model 			*dto.SearchUserListDto 				"用户查询信息"
// Param    svc 			service.UserInterface 				"用户服务接口"
// Auth      				alittlexincan@163.com         		时间（2024/07/22 11:36）
func searchUserListEndpoint(model *dto.SearchUserListDto, svc service.UserInterface) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (response interface{}, err error) {
		bus, busErr := svc.SearchUserList(model)
		if busErr != nil {
			return rep.Errors(busErr, nil), nil
		}

		return rep.Success(bus), nil
	}
}
